<template>
  <div class="page-wrapper">
    <!-- 顶部返回栏 -->
    <div class="top-bar">
      <span class="back-text" @click="goBack">&lt; 返回</span>
    </div>

    <!-- 景点卡片列表 -->
    <div class="places-container">
      <div class="place-card" v-for="place in places" :key="place.id" @click="goToScenic(place.id)">
        <div class="image-wrapper">
          <img :src="place.image" :alt="place.name" class="place-image" />
          <div class="place-tag">{{ place.address }}</div>
        </div>
        <div class="place-info">
          <div class="place-name-cn">{{ place.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import { Toast } from 'vant'

export default {
  data() {
    return {
      places: [], // 初始化为空
    };
  },
  methods: {
    goBack() {
      window.history.back(); // 返回上一页
    },
    goToScenic(id) {
      this.$router.push({ path: '/scenic', query: { id } }); // 跳转并传递 id 参数
    },
    async fetchPlaces() {
      try {
        const toast = Toast.loading({
          mask: true,
          message: '加载中...'
        })
        this.axios.post('/scenic/info')
        .then((response) => {
          this.places = response.data.data.map((place) => ({
            id: place.id,
            name: place.name,
            address: place.city,
            image: place.image,
          }));
          toast.clear();
        }, (error) => {
          console.log(error);
        })
      } catch (error) {
        console.error('Failed to fetch places:', error);
      }
    },
  },
  created() {
    this.fetchPlaces(); // 组件创建时请求数据
  },
};
</script>


<style scoped>
.page-wrapper {
  background: #f8f8f8;
  min-height: 100vh;
}

/* 顶部栏 */
.top-bar {
  position: sticky;
  top: 0;
  background-color: #ffffff;
  padding: 12px 16px;
  font-size: 15px;
  color: #333;
  z-index: 100;
  border-bottom: 1px solid #eee;
}

.back-text {
  cursor: pointer;
  user-select: none;
}

.places-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  padding: 12px;
}

@media (max-width: 340px) {
  .places-container {
    grid-template-columns: 1fr;
  }
}

.place-card {
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: transform 0.2s;
}

.place-card:hover {
  transform: translateY(-2px);
}

.image-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.place-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.place-tag {
  position: absolute;
  bottom: 6px;
  left: 6px;
  background-color: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 10px;
  padding: 2px 5px;
  border-radius: 4px;
}

.place-info {
  padding: 12px 10px;
  text-align: left;
}

.place-name-cn {
  font-size: 15px;
  font-weight: bold;
  color: #222;
  margin-bottom: 4px;
  line-height: 1.2;
}
</style>
